package com.ws.shop.action.admin;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Ticket;
import com.ws.shop.service.TicketService;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/ticket/*")
public class TicketAction extends BaseAdminAction{

	private static Logger log = Logger.getLogger(TicketAction.class);
	@Autowired
	private TicketService ticketService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		try{
			Page page = ticketService.findTicketListPage(pageno,25);
			model.addAttribute(Page.PAGE, page);
			model.addAttribute("stateDescs", Ticket.STATE_DESCS);
			return "ticket_index";
		}catch(Exception e){
			log.error("查询卡列表失败",e);
			model.addAttribute(ERROR_MSG, "系统异常，请稍后再试");
			return ERROR;
		}
	}
	
	@RequestMapping(value="add.jhtml",method=RequestMethod.GET)
	public String add(){
		return "ticket_add";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int num = RequestUtil.getIntParameter(request, "num", 0);
		int snlength = RequestUtil.getIntParameter(request, "snlength", 0);
		int sntype = RequestUtil.getIntParameter(request, "sntype", 0);
		int pwdlength = RequestUtil.getIntParameter(request, "pwdlength", 0);
		int pwdtype = RequestUtil.getIntParameter(request, "pwdtype", 0);
		int beat = RequestUtil.getIntParameter(request, "beat", 0);
		int pointb = RequestUtil.getIntParameter(request, "pointb", 0);
		String starttime = RequestUtil.getStringParameter(request, "starttime", null);
		String endtime = RequestUtil.getStringParameter(request, "endtime", null);
		if(num == 0 || snlength == 0 || pwdlength == 0 
				|| starttime == null || endtime == null){
			return this.ajaxJsonErrorMessage(response, "各项都必须");
		}
		if(beat == 0 && pointb == 0){
			return this.ajaxJsonErrorMessage(response, "缺少参数");
		}
		if(snlength < 6){
			return this.ajaxJsonErrorMessage(response, "卡号长度最少六位");
		}
		Map<String,String> ticketInfo = Ticket.getNewTicket(snlength,sntype,pwdlength,pwdtype,num);
		try{
			ticketService.saveTickets(ticketInfo,beat,pointb,
					starttime == null?new Date():DateUtil.stringToDate(starttime, DateUtil.YYYY_MM_DD_HH_MM_SS),
					endtime == null?DateUtil.newDate(new Date(), 30):DateUtil.stringToDate(endtime,DateUtil.YYYY_MM_DD_HH_MM_SS));
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("操作失败",e);
			return this.ajaxJsonErrorMessage(response, "操作失败");
		}
	}
	
	@RequestMapping(value="edit.jhtml",method=RequestMethod.GET)
	public String edit(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			model.addAttribute(ERROR_MSG, "请选择要编辑的点券");
			return ERROR;
		}
		Ticket ticket = ticketService.findTicketById(id);
		model.addAttribute("ticket", ticket);
		return "ticket_edit";
	}
	
	@RequestMapping(value="update.jhtml",method=RequestMethod.POST)
	public String update(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String cardsn = RequestUtil.getStringParameter(request, "cardsn", null);
		String cardpwd = RequestUtil.getStringParameter(request, "cardpwd", null);
		int beat = RequestUtil.getIntParameter(request, "beat", 0);
		int pointb = RequestUtil.getIntParameter(request, "pointb", 0);
		String starttime = RequestUtil.getStringParameter(request, "starttime", null);
		String endtime = RequestUtil.getStringParameter(request, "endtime", null);
		int state = RequestUtil.getIntParameter(request, "state", 1);
		Ticket ticket = new Ticket(id, cardsn, cardpwd, 
				starttime == null ? new Date():DateUtil.stringToDate(starttime, DateUtil.YYYY_MM_DD_HH_MM_SS),
				endtime == null ? DateUtil.newDate(new Date(), 30):DateUtil.stringToDate(endtime, DateUtil.YYYY_MM_DD_HH_MM_SS), beat, pointb, state);
		try{
			ticketService.updateTicket(ticket);
			return this.ajaxJsonSuccessMessage(response, "更新成功");
		}catch(Exception e){
			log.error("更新失败:"+ticket,e);
			return this.ajaxJsonErrorMessage(response, "更新失败");
		}
	}
}
